﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 创建者：王明亮
 * 创建时间：2023/8/21 11:29:25
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
namespace MyWebApp.Helper
{
    public class FileHelper
    {
        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="filePath">原文件</param>
        /// <param name="savePath">保存文件</param>
        public static void DecompressFile(string filePath, string savePath)
        {
            try
            {
                ByteToFile(Decompress(FlleToByte(filePath)), savePath);
            }
            catch (Exception ex)
            {
                File.Delete(filePath);
                File.Delete(savePath);
            }
        }

        /// <summary>
        /// 将文件转换成byte[] 数组
        /// </summary>
        /// <param name="fileUrl">文件路径文件名称</param>
        /// <returns>byte[]</returns>
        public static byte[] FlleToByte(string filePath)
        {
            FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            try
            {
                byte[] buffur = new byte[fs.Length];
                fs.Read(buffur, 0, (int)fs.Length);

                return buffur;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (fs != null)
                {
                    //关闭资源
                    fs.Close();
                }
            }
        }

        /// <summary>
        /// 字节转文件
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>

        public static bool ByteToFile(byte[] bytes, string filePath)
        {
            FileStream pFileStream = null;
            try
            {
                pFileStream = new FileStream(filePath, FileMode.OpenOrCreate);
                pFileStream.Write(bytes, 0, bytes.Length);
            }
            catch
            {
                return false;
            }
            finally
            {
                if (pFileStream != null)
                    pFileStream.Close();
            }
            return true;
        }

        /// <summary>
        /// 流转文件
        /// </summary>
        /// <param name="fileStream"></param>
        /// <param name="savePath"></param>
        /// <returns></returns>
        public static void StreamToFile(Stream mediaFile, string savePath)
        {
            try
            {
                using (FileStream fileStream = System.IO.File.Create(savePath))
                {
                    mediaFile.CopyTo(fileStream);
                    fileStream.Flush();
                    mediaFile.Flush();
                }
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 文件转stream
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public static Stream FileToStream(string filePath)
        {
            try
            {
                // 打开文件
                FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

                // 读取文件的 byte[]
                byte[] bytes = new byte[fileStream.Length];
                fileStream.Read(bytes, 0, bytes.Length);
                fileStream.Close();
                // 把 byte[] 转换成 Stream
                Stream stream = new MemoryStream(bytes);
                return stream;
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        /// <summary>
        /// 解压流
        /// </summary>
        /// <returns></returns>
        public static Stream DecompressStream(Stream stream)
        {
            return BytesToStream(Decompress(StreamToBytes(stream)));
        }

        /// <summary>
        /// 压缩流
        /// </summary>
        /// <returns></returns>
        public static Stream CompressStream(Stream stream)
        {
            return BytesToStream(Compress(StreamToBytes(stream)));
        }

        /// <summary>
        /// StreamToBytes
        /// </summary>
        /// <returns></returns>
        public static byte[] StreamToBytes(Stream stream)
        {
            try
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                stream.Seek(0, SeekOrigin.Begin);
                return bytes;
            }
            catch (Exception ex)
            {
            }
            return null;
        }

        /// <summary>
        /// BytesToStream
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static Stream BytesToStream(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }


        /// <summary> 
        /// Compress the byte[] 
        /// </summary> 
        /// <param name="input"></param> 
        /// <returns></returns> 
        public static byte[] Compress(byte[] input)
        {
            byte[] output;
            using (MemoryStream ms = new MemoryStream())
            {
                using (GZipStream gs = new GZipStream(ms, CompressionMode.Compress))
                {
                    gs.Write(input, 0, input.Length);
                    gs.Close();
                    output = ms.ToArray();
                }
                ms.Close();
            }
            return output;
        }

        /// <summary> 
        /// Decompress the byte[] 
        /// </summary> 
        /// <param name="input"></param> 
        /// <returns></returns> 
        public static byte[] Decompress(byte[] input)
        {
            List<byte> output = new List<byte>();
            using (MemoryStream ms = new MemoryStream(input))
            {
                using (GZipStream gs = new GZipStream(ms, CompressionMode.Decompress))
                {
                    int readByte = gs.ReadByte();
                    while (readByte != -1)
                    {
                        output.Add((byte)readByte);
                        readByte = gs.ReadByte();
                    }
                    gs.Close();
                }
                ms.Close();
            }
            return output.ToArray();
        }
    }
}
